import { Component, OnInit } from '@angular/core';
 
import { Hero } from './hero';
import { HeroService } from './hero.service';
import { Router } from '@angular/router';

@Component({
  selector: 'my-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css'],
})

export class HeroesComponent implements OnInit  {
  title = 'Tour of Heroes';
  selectedHero: Hero;
  heroes: Hero[];
 

  constructor(
    private router: Router, 
    private heroService: HeroService) { }

  ngOnInit(): void {
    this.getHeroes();
  }

  onSelect(hero: Hero): void {
    this.selectedHero = hero;
  }

  getHeroes(): void {
    this.heroService.getHeroesSlowly().then(heroes => this.heroes = heroes);
  }

  gotoDetail(): void {
    this.router.navigate(['/detail', this.selectedHero.id]);
  }

  add(name: String): void {
    name = name.trim()
    if (!name) { return; }
    this.heroService.create(name)
    .then(hero => {
        this.heroes.push(hero);
        this.selectedHero = null;
    })
  }

  delete(hero: Hero): void {
    
  }
}




